package com.lzh.runke.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.StopWatch;
import com.github.pagehelper.PageHelper;
import com.lzh.runke.core.web_base.constant.DateConstant;
import com.lzh.runke.core.web_base.helper.CSVHelper;
import com.lzh.runke.entity.TWxfwZyld;
import com.lzh.runke.entity.spec.SolidWasteSpec;
import com.lzh.runke.mapper.SolidWasteMapper;
import com.lzh.runke.service.ISolidWasteService;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author LiuZiHao
 * @date 创建时间:2023年07月12日 15:12:09
 */
@Service
public class SolidWasteServiceImpl implements ISolidWasteService {

    @Resource
    private SolidWasteMapper solidWasteMapper;

    @Override
    public ResponseEntity<byte[]> exportTransferCopyList(SolidWasteSpec spec) {
        StopWatch stopWatch = DateUtil.createStopWatch("CSV导出各项用时");
        stopWatch.start("从数据库查数据");
        PageHelper.startPage(spec.getPageNo(), spec.getPageSize(), false);
        List<TWxfwZyld> dataList = solidWasteMapper.loadTransferCopyDetailBySpec(spec.getDisposalMethods(),
                spec.getWasteForms(),
                spec.getJointOrderStatus(),
                spec.getTransfertStartTime(),
                spec.getTransferEndTime(),
                spec.getAcceptStartTime(),
                spec.getAcceptEndTime(),
                spec.getKeyword());
        stopWatch.stop();
        String[] headers = {"序号", "联单编号", "转出单位", "转出单位统一社会信用代码", "转出单位行业类别", "运输单位", "接受单位", "许可证号",
                "接收单位经营类别", "处置方式", "废物名称", "废物类别", "废物代码", "废物形态", "转移量(t)", "转移日期", "运输线路", "车牌号",
                "接收人", "接收日期", "联单状态"};
        StringBuilder fileName = new StringBuilder();
        String exportTime = LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern(DateConstant.FMT_YMD));
        fileName.append("固体废物").append("-").append(exportTime).append(".csv");
        List<Object[]> objects = Lists.newArrayList();
        int rowIndex = 0;
        stopWatch.start("生成list集合");
        for (TWxfwZyld data : dataList) {
            List<Object> list = Lists.newArrayList();
            rowIndex += 1;
            list.add(String.valueOf(rowIndex));
            list.add(data.getLdbh() + "\t");
            list.add(data.getPwdw());
            list.add(data.getXydm() + "\t");
            list.add(data.getHylb());
            list.add(data.getYsdw());
            list.add(data.getJydw());
            list.add(data.getJyxkzh() + "\t");
            list.add(data.getJylb());
            list.add(ObjectUtils.isEmpty(data.getCzfs()) ? "-" : data.getCzfs().getText());
            list.add(data.getFwmc());
            list.add(ObjectUtils.isEmpty(data.getFwlx()) ? "-" : data.getFwlx().getHazardouswasteTypeName());
            list.add(ObjectUtils.isEmpty(data.getFwlx()) ? "-" : data.getFwlx().getHazardouswasteTypeCode());
            list.add(data.getFwxt());
            list.add(data.getSl());
            list.add(ObjectUtils.isEmpty(data.getYsrq()) ? "-" : DateFormatUtils.format(data.getYsrq(), DateConstant.FMT_YMD) + "\t");
            list.add(data.getYsxl());
            list.add(data.getCph());
            list.add(data.getJsr());
            list.add(ObjectUtils.isEmpty(data.getJsrq()) ? "-" : DateFormatUtils.format(data.getJsrq(), DateConstant.FMT_YMD) + "\t");
            list.add(ObjectUtils.isEmpty(data.getLdzt()) ? "-" : data.getLdzt().getText());

            Object[] object = new Object[list.size()];
            list.toArray(object);
            objects.add(object);
        }
        stopWatch.stop();
        stopWatch.start("导出");
        ResponseEntity<byte[]> responseEntity = CSVHelper.writeAsHttpResponse(fileName.toString(), objects, headers);
        stopWatch.stop();
        System.out.println(stopWatch.prettyPrint(TimeUnit.SECONDS));
        return responseEntity;
    }
}
